package org.ccay.enumerable.wsrs;

import javax.ws.rs.ext.ParamConverter;

import org.ccay.enumerable.EnumItem;
import org.ccay.enumerable.EnumUtil;
import org.ccay.enumerable.exception.NoEnumItemFoundException;
import org.ccay.enumerable.exception.NoEnumTypeFoundException;

public class EnumerableConverter implements ParamConverter<EnumItem>{

	private Class<?> type; 

	public EnumerableConverter(Class<?> rawType) {
		type = rawType ;
	}

	@Override
	public EnumItem fromString(String value) {
		//是枚举类型
		if(type.isEnum()){
			EnumItem result = EnumUtil.getInstance().getByTypeValue(type.getSimpleName(), value);
			if(result == null){
				throw new NoEnumItemFoundException(type.getSimpleName(), value);
			}
			return result;
		}
		throw new NoEnumTypeFoundException();
	}
	@Override
	public String toString(EnumItem value) {
		return value.getValue();
	}
}
